package com.xuxingcan.findmyvoice.Activity;

import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;
import com.xuxingcan.findmyvoice.R;

/**
 * Created by xuxingcan on 16-3-30.
 */
public class BaseActivity extends FragmentActivity {
    public void showShortToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void showLongToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    public void showToast(String msg, int time) {
        Toast.makeText(this, msg, time).show();
    }

    public interface DialogCallback {
        boolean onPositiveActionClicked(DialogFragment fragment);

        boolean onNegativeActionClicked(DialogFragment fragment);
    }

    public void showDialog(String title, String positive) {
        showDialog(title, null, positive, null, null);
    }

    public void showDialog(String title, String message, String positive) {
        showDialog(title, message, positive, null, null);
    }

    public void showDialog(String title, String message, String positive, String negative) {
        showDialog(title, message, positive, negative, null);
    }

    public void showDialog(String title, String message, String positive, String negative, final DialogCallback callback) {
        SimpleDialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                if (callback != null) {
                    if (!callback.onPositiveActionClicked(fragment)) {
                        super.onPositiveActionClicked(fragment);
                    }
                } else {
                    super.onPositiveActionClicked(fragment);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                if (callback != null) {
                    if (!callback.onNegativeActionClicked(fragment)) {
                        super.onNegativeActionClicked(fragment);
                    }
                } else {
                    super.onNegativeActionClicked(fragment);
                }
            }
        };
        if (title != null) {
            builder.title(title);
        }
        if (message != null) {
            builder.message(message);
        }
        if (positive != null) {
            builder.positiveAction(positive);
        }
        if (negative != null) {
            builder.negativeAction(negative);
        }
//        builder.message("Let Google help apps determine location. This means sending anonymous location data to Google, even when no apps are running.")
//                .title("发生了点错误")
//                .positiveAction("我知道了")
//                .negativeAction("");
        DialogFragment fragment = DialogFragment.newInstance(builder);
        fragment.show(getSupportFragmentManager(), null);
    }

    public void showDialog(String title, int contentViewRes, String positive, String negative, final DialogCallback callback) {
        SimpleDialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                if (callback != null) {
                    if (!callback.onPositiveActionClicked(fragment)) {
                        super.onPositiveActionClicked(fragment);
                    }
                } else {
                    super.onPositiveActionClicked(fragment);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                if (callback != null) {
                    if (!callback.onNegativeActionClicked(fragment)) {
                        super.onNegativeActionClicked(fragment);
                    }
                } else {
                    super.onNegativeActionClicked(fragment);
                }
            }
        };
        if (title != null) {
            builder.title(title);
        }
        builder.contentView(contentViewRes);
        if (positive != null) {
            builder.positiveAction(positive);
        }
        if (negative != null) {
            builder.negativeAction(negative);
        }
        DialogFragment fragment = DialogFragment.newInstance(builder);
        fragment.show(getSupportFragmentManager(), null);
    }
}
